/**
 * Created by zhangqingxi on 2017/3/16.
 */

/**
 * 加载左侧grid
 * @param deptId
 */
function loadChangeDutyList(deptId) {
    $('#changeDutyList').datagrid({
        url:ROOT_PATH+'/permission/departmentDuty/findByParam.shtml',
        queryParams:{
            deptId:deptId,
            flag:"true"
        },
        idField:'id',
        fitColumns:true,
        rownumbers:true,
        striped:true,
        fit:true,
        columns:[[
            {field:'id',width:100,checkbox:true},
            {field:'name',title:'名称',width:100},
            {field:'remarks',title:'备注',width:100}
        ]]

    });
}
/**
 * 加载右侧grid
 * @param deptId
 */
function loadNoChangeDutyList(deptId) {
    $('#noChangeDutyList').datagrid({
        url:ROOT_PATH+'/permission/departmentDuty/findByParam.shtml',
        queryParams:{
            deptId:deptId,
            flag:"false"
        },
        idField:'id',
        fitColumns:true,
        rownumbers:true,
        striped:true,
        fit:true,
        columns:[[
            {field:'id',width:100,checkbox:true},
            {field:'name',title:'名称',width:100},
            {field:'remarks',title:'备注',width:100}
        ]]
    });
}
/**
 * 加载组织机构Tree
 */
function loadTree() {

    var $departmentTree=$('#departmentTree');


    //初始化组织机构树
    $departmentTree.tree({
        url:ROOT_PATH+'/permission/department/getDepartmentTree.shtml?parentId=0',
        lines:true,
        onSelect:function (node) {
            loadChangeDutyList(node.id);
            loadNoChangeDutyList(node.id);
        },
        onLoadSuccess:function (node, data) {
            var id=data[0].id;
            //找到第一个元素
            var selectedTreeNode = $departmentTree.tree('find', id);
            //调用选中事件
            $departmentTree.tree('select', selectedTreeNode.target);
        }

    });
}
function handlerGrid(insertId,removeId) {

    var $insertGrid=$('#'+insertId);
    var $removeGrid=$('#'+removeId);
    //获得选中的数据
    var checkedRows=$removeGrid.datagrid('getChecked');
    for(var i=0;i<checkedRows.length;i++){

        var row=checkedRows[i];

        var id=row.id;
        var rowIndex=$removeGrid.datagrid('getRowIndex',row);
        var name=row.name;
        var remarks=row.remarks;
        $insertGrid.datagrid('insertRow',{
            index: i,	// 索引从0开始
            row: {
                id: id,
                name: name,
                remarks: remarks
            }
        });

        $removeGrid.datagrid('deleteRow',rowIndex);

        i--;

    }
}
$(function () {

    loadTree();
    
    $("#btn_departmentDuty_left").on('click',function () {

        var insertId='changeDutyList';
        var removeId='noChangeDutyList';
        handlerGrid(insertId,removeId);



    });

    $("#btn_departmentDuty_right").on('click',function () {
        var insertId='noChangeDutyList';
        var removeId='changeDutyList';
        handlerGrid(insertId,removeId);
    });

    $("#btn_departmentDuty_save").on('click',function () {

        //获得职位Id集合
        var $ChangeDutyList=$('#changeDutyList');
        var data=$ChangeDutyList.datagrid('getRows');
        var dutyIdArray=[];
        for(var i=0;i<data.length;i++){
            var dutyId=data[i].id;
            dutyIdArray.push(dutyId);
        }

        //获得部门id
        var $departmentTree=$('#departmentTree');

        var selectedTreeNode=$departmentTree.tree('getSelected');

        var deptId=selectedTreeNode.id;

        layer.confirm('是否确定保存？', {
            btn: ['确定','取消'] //按钮
        }, function(){
            $.ajax({
                type: "POST",
                url: ROOT_PATH+'/permission/departmentDuty/add.shtml',
                data: {dutyIds:dutyIdArray.join(),deptId:deptId},
                dataType: "json",
                success: function(data){
                    var icon=1;
                    if(!data.success){
                        icon=2;
                    }
                    layer.msg(data.message,{time:1000,icon:icon});
                }
            });
        });

    })

});